package com.service.socket.process;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.jboss.netty.channel.Channel;

import com.service.cache.CacheManager;
import com.service.other.Logger;
import com.service.socket.protocol.CmdWord;
import com.service.socket.protocol.Conversion;
import com.service.socket.protocol.Packet;

public class R_Class_FA extends R_Class_Abstract {

	public R_Class_FA(Channel channel, Packet packet) {
		super(channel, packet);
	}

	@Override
	public void execute() {
		try {
			int cmdword = Integer.parseInt(packet.cmdWord, 16);
			switch (cmdword) {
			case 0x01:
				method_01();
				break;
			case 0x02:
				method_02();
				break;
			case 0x03:
				method_03();
				break;
			case 0x04:
				method_04();
				break;
			case 0x05:
				method_05();
				break;
			case 0x06:
				method_06();
				break;
			case 0xFA:
				method_FA();
				break;
			}
		} catch (Exception e) {
			Logger.err(e);
		}
	}

	//解绑指定GPSNO
	public void method_02(){
		if (!packet.isCrcValid()) {
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device [" + packet.getDeviceNo()+ "] removeVehicleMapByGps fail on [isCrcValid]:" + packet.isCrcValid());
			return;
		}
		String gpsno = new String(Conversion.hexStringToBytes(packet.dataArea));
		CacheManager.instance().removeVehicleMapByGps(gpsno);
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyCtrl(channel,packet);
		//为了垃圾称重相关逻辑，主动切断车辆的连接，等车辆加绑走A0分支，能触发指令下发
		if(CacheManager.instance().sessionMap.containsKey(gpsno)){
			Integer channelID = CacheManager.instance().sessionMap.get(gpsno);			
			if (CacheManager.instance().activeChannel.containsKey(channelID)) {
				CacheManager.instance().activeChannel.get(channelID).close();
			}
		}
	}

	//加绑指定GPSNO
	public void method_01(){
		if (!packet.isCrcValid()) {
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device [" + packet.getDeviceNo()+ "] addVehicleMapByGps fail on [isCrcValid]:" + packet.isCrcValid());
			return;
		}
		String gpsno = new String(Conversion.hexStringToBytes(packet.dataArea));
		boolean bReturn = CacheManager.instance().addVehicleMapByGps(gpsno);
		if(!bReturn){
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device [" + packet.getDeviceNo()+ "] addVehicleMapByGps fail on addVehicleMapByGps");
			return;
		}
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyCtrl(channel,packet);
		//等待下发的GPS
		CacheManager.instance().afterFaSet.add(gpsno);
	}
	
	/**
	 * 新增排班
	 */
	public void method_04() {
		if (!packet.isCrcValid()) {
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device [" + packet.getDeviceNo()+ "] addVehicleMapByGps fail on [isCrcValid]:" + packet.isCrcValid());
			return;
		}
		String gpsNos = new String(Conversion.hexStringToBytes(packet.dataArea));
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyCtrl(channel,packet);
		if (null != gpsNos && 0 != gpsNos.trim().length()) {
			List<String> gpsNoList = null;
			if (gpsNos.contains(",")) {
				gpsNoList = Arrays.asList(gpsNos.split(","));
			} else {
				gpsNoList = new ArrayList<String>();
				gpsNoList.add(gpsNos);
			}
			if (null != gpsNoList && 0 != gpsNoList.size()) {
				CacheManager.instance().addDutyByGps(gpsNoList);
			}
		}
	}
	
	/**
	 * 删除排班
	 */
	public void method_03() {
		if (!packet.isCrcValid()) {
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device [" + packet.getDeviceNo()+ "] addVehicleMapByGps fail on [isCrcValid]:" + packet.isCrcValid());
			return;
		}
		String gpsNos = new String(Conversion.hexStringToBytes(packet.dataArea));
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyCtrl(channel,packet);
		if (null != gpsNos && 0 != gpsNos.trim().length()) {
			List<String> gpsNoList = null;
			if (gpsNos.contains(",")) {
				gpsNoList = Arrays.asList(gpsNos.split(","));
			} else {
				gpsNoList = new ArrayList<String>();
				gpsNoList.add(gpsNos);
			}
			if (null != gpsNoList && 0 != gpsNoList.size()) {
				CacheManager.instance().deleteDutyByGps(gpsNoList);
			}
		}
	}
	
	/**
	 * 加绑勾臂车
	 */
	public void method_05() {
		if (!packet.isCrcValid()) {
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device [" + packet.getDeviceNo()+ "] addHookArmCaseByGps fail on [isCrcValid]:" + packet.isCrcValid());
			return;
		}
		String gpsno = new String(Conversion.hexStringToBytes(packet.dataArea));
		boolean bReturn = CacheManager.instance().addHookArmCaseMapByGps(gpsno);
		if(!bReturn){
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device [" + packet.getDeviceNo()+ "] addHookArmCaseByGps fail on addVehicleMapByGps");
			return;
		}
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyCtrl(channel,packet);
	}
	
	/**
	 * 解绑勾臂车
	 */
	public void method_06() {
		if (!packet.isCrcValid()) {
			packet.status = CmdWord.STATUS_FAIL;
			packet.dataArea = null;
			SendCommand.replyDtu(channel,packet);
			Logger.err("device [" + packet.getDeviceNo()+ "] removeHookArmCaseMapByGps fail on [isCrcValid]:" + packet.isCrcValid());
			return;
		}
		String gpsno = new String(Conversion.hexStringToBytes(packet.dataArea));
		CacheManager.instance().removeHookArmCaseMapByGps(gpsno);
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyCtrl(channel,packet);
	}
	
	public void method_FA(){
		CacheManager.instance().refreshVehicleMap();
		packet.status = CmdWord.STATUS_SUCCESS;
		packet.dataArea = null;
		SendCommand.replyCtrl(channel,packet);
	}
}
